class VotqController < ApplicationController
  def index
    @category = Category.all
    require 'pp'
    pp "------------"
    pp params
    #    return render :text =>"The Object is #{params}"
    if !params[:cat].nil?
      @statement  = Statement.paginate(:all, :per_page => 1,:page => params[:page],:conditions =>["category_id = ?",params[:cat]])
      @badday  = Badday.paginate(:all, :per_page => 1,:page => params[:page],:conditions =>["category_id = ?",params[:cat]])
      @question = Question.paginate(:all, :per_page => 1,:page => params[:page],:conditions =>["category_id = ?",params[:cat]])
    else
      @statement  = Statement.paginate(:all, :per_page => 1,:page => params[:page])
      @badday = Badday.paginate(:all, :per_page => 1,:page => params[:page])
      @question = Question.paginate(:all, :per_page => 1,:page => params[:page])
    end
  end

  def statement
    @category = Category.all
    if !params[:cat].nil?
      @statement  = Statement.paginate(:all, :per_page => 1,:page => params[:page],:conditions =>["category_id = ?",params[:cat]])
    else
      @statement  = Statement.paginate(:all, :per_page => 1,:page => params[:page])
    end
  end

  def badday
    @category = Category.all
    if !params[:cat].nil?
      @badday  = Badday.paginate(:all, :per_page => 1,:page => params[:page],:conditions =>["category_id = ?",params[:cat]])
    else
      @badday = Badday.paginate(:all, :per_page => 1,:page => params[:page])
    end
  end

  def question
    @category = Category.all
    if !params[:cat].nil?
      @question = Question.paginate(:all, :per_page => 1,:page => params[:page],:conditions =>["category_id = ?",params[:cat]])
    else
      @question = Question.paginate(:all, :per_page => 1,:page => params[:page])
    end
  end

  def comment_vote_up
    @comment = Comment.find_by_id(params[:format])
    voter = CommentVoter.find(:all,:conditions =>["remote_voter =? AND comment_id=?",cookies[:ip],@comment.id])
    if voter.blank?
      @question = Question.find_by_id(@comment.question_id)
      vote_for_comment = CommentVoter.new
      vote_for_comment.comment_id = @comment.id
      vote_for_comment.member_voter = 0
      vote_for_comment.remote_voter = cookies[:ip]
      @comment.comment_voters << (vote_for_comment)
      vote_for_comment.save
      @comment.update_attribute(:agree,Integer(@comment.agree) + 1)
      flash[:comment_ss] = "Thank you for voting the comment"
    else
      flash[:comment_err] = "You have vote for this comment"
    end
    redirect_to :action => "index"
  end

  def comment_vote_down
    @comment = Comment.find_by_id(params[:format])
    voter = CommentVoter.find(:all,:conditions =>["remote_voter =? AND comment_id=?",cookies[:ip],@comment.id])
    if voter.blank?
      @question = Question.find_by_id(@comment.question_id)
      vote_for_comment = CommentVoter.new
      vote_for_comment.comment_id = @comment.id
      vote_for_comment.member_voter = 0
      vote_for_comment.remote_voter = cookies[:ip]
      @comment.comment_voters << (vote_for_comment)
      vote_for_comment.save
      @comment.update_attribute(:disagree,Integer(@comment.disagree) + 1)
      flash[:comment_ss] = "Thank you for voting the comment"
    else
      flash[:comment_err] = "You have vote for this comment"
    end
    redirect_to :action => "index"
  end
end